package com.base.data.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.data.dao.QxDao;
import com.base.data.entity.Qx;
import com.mysteel.page.Page;
import com.mysteel.util.ObjectUtils;

@Service("qxService")
public class QxServiceImpl implements QxService
{
	@Autowired
	private QxDao qxDao;
	
	@Override
	public Qx getQxById(long id)
	{
		
		return qxDao.getById (id);
	}
	
	@Override
	public Qx getQxByUrl(String url)
	{
		
		return qxDao.getByUrl (url);
	}
	
	@Override
	public List<Qx> getQxByParams(Map<String, Object> params)
	{
		
		List<Qx> list = new ArrayList<Qx> ();
		
		for( long id : qxDao.getByParams (params))
		{
			
			list.add (qxDao.getById (id));
		}
		
		return list;
	}
	
	@Override
	public void deleteQxById(long id)
	{
		
		qxDao.delete (id);
	}
	
	@Override
	public void saveQx(Qx qx)
	{
		if (ObjectUtils.notNull (qx))
		{
			qx.setCreateTime (System.currentTimeMillis ());
			// qx.setAuthorName (authorName);
			qx.setLastAccess (System.currentTimeMillis ());
			qx.setStatus (true);
			qxDao.save (qx);
		}
		
	}
	
	@Override
	public void updateQx(Qx qx)
	{
		Qx q = qxDao.getById (qx.getId ());
		
		if (ObjectUtils.notNull (q))
		{
			q.setName (qx.getName ());
			q.setParentId (qx.getParentId ());
			q.setDescription (qx.getDescription ());
			q.setLastAccess (System.currentTimeMillis ());
			q.setStatus (qx.isStatus ());
			
			qxDao.update (q);
		}
		
	}
	
	@Override
	public Page<Qx> page(Map<String, Object> params, int currentPageNum,int maxPageRowCount)
	{
		
		Page<Qx> page = null;
		
		if (ObjectUtils.isNull (params))
		{
			params = new HashMap<String, Object> (2);
		}
		
		int count = qxDao.count (params);
		
		if (count > 0)
		{
			page = new Page<Qx> ();
			page.setTotalRowCount (count);
			page.setCurrentPageNum (currentPageNum);
			page.setMaxPageRowCount (maxPageRowCount);
			page.setURL (params);
			
			params.put ("startPageRowCount" ,page.getStartPageRowCount ());
			params.put ("maxPageRowCount" ,page.getMaxPageRowCount ());
			
			List<Long> qxs = qxDao.page (params);
			
			if (ObjectUtils.notEmpty (qxs))
			{
				List<Qx> qx = new ArrayList<Qx> (qxs.size ());
				
				for( long qxId : qxs)
				{
					qx.add (qxDao.getById (qxId));
				}
				
				page.setDatas (qx);
			}
		}
		
		return page;
	}

	@Override
	public void deleteAllQx()
	{
		qxDao.deleteAll ();
	}
	
}
